package com.feimt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.feimt.utils.BaseContent;
import com.feimt.pojo.AddressBook;
import com.feimt.result.R;
import com.feimt.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 宋孟林
 * @description
 */
@RestController
@Slf4j
@RequestMapping("/addressBook")
public class AddressBookController {
    private final AddressBookService addressBookService;

    @Autowired
    public AddressBookController(AddressBookService addressBookService) {
        this.addressBookService = addressBookService;
    }

    /**
     * 点击地址铺，将当前登陆的用户的已经设置的地址数据显示在页面中
     * @param addressBook 传入的addressBook对象
     * @return 返回查询的集合
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(BaseContent.getThreadLocal());
        Long id = addressBook.getUserId();
        log.info("addressBook: {}", addressBook);
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id != null, AddressBook::getUserId, id);
        wrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }

    /**
     * 点击修改将想要更改的数据回显到显示框中
     * @param id 需要回显的地址id
     * @return 返回数据对象
     */
    @GetMapping("/{id}")
    public R<AddressBook> getData(@PathVariable Long id){
        log.info("回显的地址信息的id为：{}", id);
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 将修改的地址对象保存到数据库中
     * @param addressBook 当前提交的修改后的地址对象
     * @return 是否修改成功
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContent.getThreadLocal());
        log.info("当前修改地址对象addressBook为：{}", addressBook);
        boolean flag = addressBookService.updateById(addressBook);
        return flag ? R.success("修改成功") : R.error("修改失败");
    }

    /**
     * 新增用户地址
     * @param addressBook 地址对象
     * @return 返回前端信息
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContent.getThreadLocal());
        log.info("需要保存的地址为：{}", addressBook);
        boolean flag = addressBookService.save(addressBook);
        return flag ? R.success("增加成功") : R.error("增加失败");
    }

    /**
     * 设置默认地址
     * @param addressBook 地址对象
     * @return 更改后的对象
     */
    @PutMapping("/default")
    public R<AddressBook> setDefaultAddress(@RequestBody AddressBook addressBook){
        log.info("设置默认地址是： {}", addressBook);
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContent.getThreadLocal()).set(AddressBook::getIsDefault, 0);
        addressBookService.update(wrapper);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    @GetMapping("/default")
    public R<AddressBook> getDefaultAddress(AddressBook addressBook){
        log.info("设置默认地址是： {}", addressBook);
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContent.getThreadLocal()).eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook1 = addressBookService.getOne(wrapper);
        return R.success(addressBook1);
    }

    /**
     * 删除地址功能
     * @param ids 需要删除的地址的id
     * @return 返回是否删除成功
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除地址的id为, {}", ids);
        boolean flag = addressBookService.removeByIds(ids);
        return flag ? R.success("删除成功") : R.error("删除失败");
    }
}
